<?php
/**
 * Created by PhpStorm.
 * Power By Mikkle
 * Email：776329498@qq.com
 * Date: 2017/11/7
 * Time: 11:04
 */

namespace app\console\command\webim;


use app\common\library\token\driver\Redis;
use mikkle\tp_swoole\WebsocketServer;
use think\Request;
use think\Db;
use app\common\library\Token;
use app\common\library\Auth;

class WebSocket extends WebsocketServer
{

    protected $redis;
    protected $auth = null;
    /**
     * 初始化的回调方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param \swoole_websocket_server $server
     */
    public function initialize(\swoole_websocket_server $server)
    {
        $this->redis = new \think\cache\driver\Redis();
        $this->auth = Auth::instance();
    }

    /**
     * 创建链接时候的回调方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param \swoole_websocket_server $server
     * @param \swoole_http_request $request
     */
    public function onOpen(\swoole_websocket_server $server, \swoole_http_request $request)
    {
        echo "connection open:{$request->fd}\n";
    }


    /**
     * 接收信息回调方法
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param \swoole_websocket_server $server
     * @param \swoole_websocket_frame $frame
     */
    // 收到数据时回调函数
    public function onMessage(\swoole_websocket_server $server, \swoole_websocket_frame $frame)
    {

        $receive_data = json_decode($frame->data);
        if( $receive_data->type == 'ping' ){
            echo '心跳检测';
        }
        if($receive_data->type=='bind'){
            //关系绑定
            $this->redis->set('fd_'.$receive_data->uid,$frame->fd);
            $this->redis->set('fdname_'.$frame->fd,$receive_data->uid);

        }elseif( $receive_data->type == 'bindUser' ) {
            $this->redis->set('homefd_'.$receive_data->uuid,$frame->fd);
            $this->redis->set('fdhome_'.$frame->fd,$receive_data->uuid);
            $server->push($frame->fd,'success');
        }
    }


    public function onRequest(\swoole_websocket_server $server, \swoole_http_request $request, \swoole_http_response $response)
    {
        $data = $request->post;
        $uids = Db::name('admin')->column('id');
        if( $data['type'] == 'sendhome' ){ //前端用户推送

            $uuid = $data['uuid'];
            $fd = (int)$this->redis->get('homefd_'.$uuid);
            if( $fd > 0 ){
                $server->push($fd,json_encode($data['message'],JSON_UNESCAPED_UNICODE));
            }

        }elseif($data['type'] == 'bet'){    //投注
            foreach ( $uids as $uid ){ //后端管理员推送
                $fd = (int)$this->redis->get('fd_'.$uid);
                if( $fd > 0 ){
                    $data['message'] = "账号:{$data['username']}下注:{$data['money']}";
                    $server->push($fd, json_encode($data));
                }
            }
        }elseif ( $data['type'] == 'tixian' ){
            foreach ( $uids as $uid ){ //后端管理员推送
                $fd = (int)$this->redis->get('fd_'.$uid);
                if( $fd > 0 ){
                    $data['message'] = "账号:{$data['username']}提现:{$data['money']}";
                    $server->push($fd, json_encode($data));
                }
            }
        }




    }

    // 连接关闭时回调函数
    public function onClose($server, $fd)
    {
        echo "client {$fd} closed\n";
        //取消关系绑定
        $redis = new \think\cache\driver\Redis();

        $redis->rm('fd_'.$redis->get('fdname_'.$fd));//删除fd_uid键的缓存 后端
        $redis->rm('fdname_'.$fd);//删除fd_name 缓存

        $redis->rm('homefd_'.$redis->get('fdhome_'.$fd));// 前端
        $redis->rm('fdhome_'.$fd);

    }


}